from src.baseapp.controller.baseController import BaseController
from src.baseapp.model.BaseOrmOpeator import OrmOperator
from src.baseapp.model.testcaseServiceModel.testcaseDirectoryModel import TestCaseDirectoryModel


class DirectoryController(BaseController):
    def __init__(self):
        super().__init__()
        self.orm = OrmOperator(TestCaseDirectoryModel)

    def __get_son_directory_by_id(self, directory_id: int):
        """
        根据目录id获取所有下属目录
        """
        directory_ids = [directory_id]
        directories = self.orm.equal(TestCaseDirectoryModel.parent_id, directory_id).get_all()
        for directory in directories:
            directory_ids.extend(self.__get_son_directory_by_id(directory.directory_id))
        return directory_ids

    def __get_parent_directory_by_id(self, directory_id: int):
        """
        根据目录id获取所有上级目录
        """
        directory_ids = [directory_id]
        directory = self.orm.equal(TestCaseDirectoryModel.directory_id, directory_id).equal(
            TestCaseDirectoryModel.is_delete, 0).get_one()
        if directory.parent_id != -1:
            directory_ids.extend(self.__get_parent_directory_by_id(directory.parent_id))
        return directory_ids

    def get_all_directory_id_by_id(self, directory_id, is_son=True):
        """
        根据目录id获取所有下属目录id
        """
        if is_son:
            return self.__get_son_directory_by_id(directory_id)
        else:
            return self.__get_parent_directory_by_id(directory_id)

    def add_directory(self, directory_name, parent_id=-1):
        response = self.response_data()
        try:
            self.orm.insert_data(TestCaseDirectoryModel(name=directory_name, parent_id=parent_id))
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def delete_directory(self, directory_id: int):
        response = self.response_data()
        try:
            directory_ids = self.get_all_directory_id_by_id(directory_id)
            directories = self.orm.in_here(TestCaseDirectoryModel.directory_id, directory_ids).equal(
                TestCaseDirectoryModel.is_delete, 0).get_all()
            for directory in directories:
                directory.is_delete = 1
            self.orm.update_data(directories)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def update_directory(self, directory_id: int, directory_name: str):
        response = self.response_data()
        try:
            directory = self.orm.equal(TestCaseDirectoryModel.directory_id, directory_id).equal(
                TestCaseDirectoryModel.is_delete, 0).get_one()
            directory.name = directory_name
            self.orm.update_data(directory)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def get_all_directory(self):
        response = self.response_data()
        try:
            directories = self.orm.equal(TestCaseDirectoryModel.is_delete, 0).get_all()
            response.data['directories'] = [
                {
                    'directory_id': directory.directory_id,
                    'name': directory.name,
                    'parent_id': directory.parent_id,
                }
                for directory in directories
            ]
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def get_directory_by_id(self, directory_id: int):
        response = self.response_data()
        try:
            directory_ids = self.get_all_directory_id_by_id(directory_id)
            directories = self.orm.in_here(TestCaseDirectoryModel.directory_id, directory_ids).equal(
                TestCaseDirectoryModel.is_delete, 0).get_all()
            response.data['directories'] = [
                {
                    'directory_id': directory.directory_id,
                    'name': directory.name,
                    'parent_id': directory.parent_id,
                }
                for directory in directories
            ]
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def get_directory_by_name(self, directory_name: str):
        response = self.response_data()
        try:
            directories = self.orm.like(TestCaseDirectoryModel.name, directory_name).equal(
                TestCaseDirectoryModel.is_delete, 0).get_all()
            response.data['directories'] = [
                {
                    'directory_id': directory.directory_id,
                    'name': directory.name,
                    'parent_id': directory.parent_id,
                }
                for directory in directories
            ]
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict

    def move_directory(self, directory_id: int, target_directory_id: int):
        response = self.response_data()
        try:
            directories = self.orm.equal(TestCaseDirectoryModel.directory_id, directory_id).equal(
                TestCaseDirectoryModel.is_delete, 0).get_all()
            for directory in directories:
                directory.parent_id = target_directory_id
            self.orm.update_data(directories)
        except Exception as e:
            response.code = 500
            response.msg = str(e)
        return response.code, response.to_dict
